package com.youyou.fusheng2.View.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.youyou.fusheng2.Moudle.FuShengCallBack;
import com.youyou.fusheng2.Moudle.OnClick2;
import com.youyou.fusheng2.Moudle.sousuoShow;
import com.youyou.fusheng2.Person.FuShengMoudle;
import com.youyou.fusheng2.Person.Test.SouSuoTest;
import com.youyou.fusheng2.R;
import com.youyou.fusheng2.Utils.EveryUrl;
import com.youyou.fusheng2.View.Adapter.SouSuoAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class Sousuo_activity extends AppCompatActivity implements FuShengCallBack, sousuoShow, View.OnClickListener, OnClick2 {

    private RecyclerView              sousuo_rec;
    private List<Map<String, String>> sousuoList    = new ArrayList<>();
    private MyHander                  myHander;
    private SouSuoAdapter             souSuoAdapter;
    private FuShengMoudle             fuShengMoudle = new FuShengMoudle(this);
    private TextView                  tv_error;
    private ProgressBar               show;
    private Timer                     timer         = new Timer();
    private int                       ssj           = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sousuo_activity);
        String string = getIntent().getBundleExtra("id").getString("text");
        initView(string);
        initAdapter();
        GetSouSuo(string);
    }

    private void initAdapter() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  00:37
         *  @简介: 初始化适配器
         */
        souSuoAdapter = new SouSuoAdapter((sousuoShow) this, (OnClick2) this);
        sousuo_rec.setAdapter(souSuoAdapter);
        sousuo_rec.setLayoutManager(new GridLayoutManager(this, 1));
    }

    private void GetSouSuo(String string) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                fuShengMoudle.getUrlDate(EveryUrl.vdioSouSuo + string, 0);
            }
        }).start();
    }

    private void initView(String string) {
        myHander = new MyHander();
        findViewById(R.id.home_tetle).setVisibility(View.GONE);
        findViewById(R.id.home_sousuo).setVisibility(View.GONE);
        EditText home_sousuo_input = (EditText) findViewById(R.id.home_sousuo_input);
        home_sousuo_input.setVisibility(View.VISIBLE);
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
        sousuo_rec = (RecyclerView) findViewById(R.id.sousuo_rec);
        //把穿过来的数据显示到顶部搜索框
        home_sousuo_input.setText(string);
        //设置搜索出结果的页面不可编辑
        home_sousuo_input.setClickable(false);
        home_sousuo_input.setFocusable(false);
        TextView sousuo_show = (TextView) findViewById(R.id.sousuo_show);
        tv_error = (TextView) findViewById(R.id.tv_error);
        sousuo_show.setVisibility(View.VISIBLE);
        sousuo_show.setOnClickListener(this);
        ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
        layoutParams.height = (int) (layoutParams.height * 1.3);
        layout.setLayoutParams(layoutParams);
        show = findViewById(R.id.show);
    }

    @Override
    public void GetSuccess(String text, int mark) {
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void GetError(String text, int mark) {
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void showTextView(Boolean s) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  00:36
         *  @简介: 如果集合为空 回掉一个s
         *  当s为空的true的时候 进行开启定时器
         */
        if (s) {
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    if (ssj == 3) {
                        Message message = new Message();
                        message.what = 10;
                        myHander.sendMessage(message);
                    } else {
                        Message message = new Message();
                        message.what = 11;
                        myHander.sendMessage(message);
                    }
                    ssj++;
                }
            };
            timer.schedule(timerTask, 0, 1000);
        } else {
            show.setVisibility(View.GONE);
            tv_error.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  00:35
         *  @简介: 点击销毁页面
         */
        if (view.getId() == R.id.sousuo_show) {
            this.finish();
        }
    }

    @Override
    public void listener1(int Flage, String Text) {
        Bundle bundle = new Bundle();
        bundle.putInt("f", Integer.parseInt(Text));
        startActivity(new Intent(this, com.youyou.fusheng2.View.Activity.playVdioActivity.class).putExtra("text", bundle));
    }

    @Override
    public void listener1(int Flage, String Text, int Index) {

    }

    @SuppressLint("HandlerLeak")
    public class MyHander extends Handler {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    jiexi(msg.obj.toString());
                    Log.i("sss", "handleMessage: +" + msg.obj.toString());
                    souSuoAdapter.setSousuoList(sousuoList);
                    souSuoAdapter.notifyDataSetChanged();
                    break;
                case 10:
                    //显示是否有数据 并销毁定时器
                    show.setVisibility(View.GONE);
                    tv_error.setVisibility(View.VISIBLE);
                    tv_error.setText("暂时没有数据！");
                    timer.cancel();
                    break;
                case 11:
                    show.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    private void jiexi(String toString) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  00:34
         *  @简介: 解析搜索出的数据
         */
        Gson gson = new Gson();
        SouSuoTest souSuoTest = gson.fromJson(toString, SouSuoTest.class);
        List<SouSuoTest.DataBean> data = souSuoTest.getData();
        for (int i = 0; i < data.size(); i++) {
            Map<String, String> sousuoMap = new HashMap<>();
            sousuoMap.put("note", data.get(i).getV_note());
            sousuoMap.put("score", String.valueOf(data.get(i).getV_score()));
            sousuoMap.put("pic", data.get(i).getV_pic());
            sousuoMap.put("name", data.get(i).getV_name());
            sousuoMap.put("tid", String.valueOf(data.get(i).getV_id()));
            sousuoList.add(sousuoMap);
        }
    }


    @Override
    protected void onDestroy() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  00:32
         *  @简介: 在页面销毁的时候 进行把定时器销毁 然后其他对象职位null
         */
        super.onDestroy();
        if (myHander != null || timer != null) {
            timer.cancel();
            myHander = null;
            timer = null;
        }
    }
}